/**
 * Created by sxwu on 14-2-15.
 */


var log = require("../lib/log");
var config = require('../config');
var adinfo = require('../models/adinfo');
var crypto = require('crypto');
var RUtil = require('./dataformatUtil');
//var domain = require('domain');




exports.index = function(request, response){
    request.session.lastpage="adsother.html";
    if(request.session.admin==undefined)
    {
        response.redirect("login.html");
    }
    else
    {

        var method = request.method || '';
        var data = RUtil.ad_data(request);
        data.front_data.title = '广告服务设置';
        data.front_data.subtitle='可编辑HTML广告配置';
        data.front_data.usernick =request.session.admin.usernick;
        data.front_data.usertype =request.session.admin.usertype;


        if(method.toUpperCase() === "GET") {
            adinfo.selectHTMLByUserid(request.session.admin.userid, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    data.front_data.errDesp = "查询失败：系统内部错误(错误码："+error.errno+")";
                    data.front_data.status="danger";
                    response.render("adsother", data);
                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    data.front_data.errDesp = "现在还没有配置广告，马上开始配置吧";
                    data.front_data.status="success";
                    response.render("adsother", data);
                } else {
                    //存在数据将数据发到页面

                    data.info=JSON.stringify(results);
                    //data.info.adhtml=JSON.parse(data.info.adhtml);
                    response.render("adsother", data);
                }
            });

        }

        else if(method.toUpperCase() === "POST") {
            //post需要区分不同的oper类型
            data.adinfo.userid = request.session.admin.userid;

            if(request.body.oper == "add")
            {
                //userid,adname,adtype,adpicurl,adlinkurl,adhtml,adactivedate,adexpiredate,addevtype,
                data.adinfo.adname=request.body.adname;
                data.adinfo.adtype=1;
                data.adinfo.adhtml=request.body.adhtml;
                data.adinfo.adhtmlsrc=request.body.adhtmlsrc;
                data.adinfo.adactivedate=request.body.adactivedate;
                data.adinfo.adexpiredate=request.body.adexpiredate;
                data.adinfo.addevtype=0;

                adinfo.insert(data.adinfo, function(error,results) {
                    var optstatus="error";
                    if((error)!== null)
                    {
                        // 数据库异常
                        data.front_data.errDesp = "添加失败：系统内部错误(错误码："+error.errno+")";
                    }
                    else if(results.length <= 0) {
                        //没有该虚拟运营商的配置信息，响应空的内容
                        data.front_data.errDesp = "添加失败：数据库操作异常！";
                    } else {
                        data.front_data.errDesp="广告配置成功！";
                        optstatus="success";
                    }
                    var info = { status:optstatus,info:data.front_data.errDesp };
                    response.write(JSON.stringify(info));
                    response.end();
                });
            }
            else if(request.body.oper == "edit")
            {
                //userid,adname,adtype,adpicurl,adlinkurl,adhtml,adactivedate,adexpiredate,addevtype,
                data.adinfo.userid=request.body.userid;
                data.adinfo.adid=request.body.id;
                data.adinfo.adname=request.body.adname;
                data.adinfo.adtype=1;
                data.adinfo.adhtml=request.body.adhtml;
                data.adinfo.adhtmlsrc=request.body.adhtmlsrc;
                data.adinfo.adactivedate=request.body.adactivedate;
                data.adinfo.adexpiredate=request.body.adexpiredate;
                data.adinfo.addevtype=0;

                adinfo.update(data.adinfo, function(error,results) {
                    var optstatus="error";
                    if((error)!== null)
                    {
                        // 数据库异常
                        data.front_data.errDesp = "更新失败：系统内部错误(错误码："+error.errno+")";
                    }
                    else if(results.length <= 0) {
                        //没有该虚拟运营商的配置信息，响应空的内容
                        data.front_data.errDesp = "更新失败：数据库操作异常！";
                    } else {
                        data.front_data.errDesp="广告更新成功！";
                        optstatus="success";
                    }
                    var info = { status:optstatus,info:data.front_data.errDesp };
                    response.write(JSON.stringify(info));
                    response.end();
                });
            }
            else if(request.body.oper == "del")
            {
                data.adinfo.userid=request.body.userid;
                var delid= new Array();
                delid = request.body.id.split(',');
                adinfo.delete(delid, function(error,results) {
                    var optstatus="error";
                    if((error)!== null)
                    {
                        // 数据库异常
                        data.front_data.errDesp = "删除失败：系统内部错误(错误码："+error.errno+")";
                    }
                    else if(results.length <= 0) {
                        //没有该虚拟运营商的配置信息，响应空的内容
                        data.front_data.errDesp = "删除失败：数据库操作异常！";
                    } else {
                        data.front_data.errDesp="广告删除成功！";
                        optstatus="success";
                    }
                    var info = { status:optstatus,info:data.front_data.errDesp };
                    response.write(JSON.stringify(info));
                    response.end();
                });
            }



        }
    }
};
